package leetcode.L500;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//hashmap使用


public class EX17 {
    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        int deep = 0;
        if(digits.length()==0){
            return combinations;
        }
        HashMap<Character,String> map = new HashMap<Character, String>(){
            {
                put('2', "abc");
                put('3', "def");
                put('4', "hgi");
                put('5', "jkl");
                put('6', "mno");
                put('7', "pqrs");
                put('8', "tuv");
                put('9', "wxyz");
            }
        };
        dfs(digits,combinations,map,deep,new StringBuffer());
        return combinations;
    }

    private void dfs(String digits, List<String> combinations, HashMap<Character, String> map, int deep,StringBuffer path) {
        if(deep == digits.length()){
            combinations.add(path.toString());
            return;
        }

        String str = map.get(digits.charAt(deep));
        for (int i = 0; i <str.length() ; i++) {
            path.append(str.charAt(i));
            dfs(digits,combinations,map,deep+1,path);
            path.deleteCharAt(path.length()-1);
        }
    }

    public static void main(String[] args) {
        String digits = "23";
        EX17 solution = new EX17();
        List<String> lists = solution.letterCombinations(digits);
        System.out.println(lists);
    }
}
